<template>
  <div class="doctor-detail">
    <van-nav-bar
      title="医生详情"
      left-arrow
      @click-left="router.back()"
      fixed
    />

    <!-- 医生基本信息 -->
    <div class="doctor-card">
      <van-cell-group inset>
        <van-cell center>
          <template #icon>
            <van-image
              round
              width="60"
              height="60"
              :src="doctorInfo.avatar || defaultAvatar"
              style="margin-right: 12px"
            />
          </template>
          <template #title>
            <div class="doctor-info">
              <span class="name">{{ doctorInfo.realName }}</span>
              <van-tag type="primary" v-if="doctorInfo.isAuth === 1">已认证</van-tag>
            </div>
          </template>
          <template #label>
            <div class="status">
              <van-tag :type="doctorInfo.online ? 'success' : 'warning'">
                {{ doctorInfo.online ? '在线' : '离线' }}
              </van-tag>
            </div>
          </template>
        </van-cell>
      </van-cell-group>
    </div>

    <!-- 医生统计信息 -->
    <div class="stats-card">
      <van-cell-group inset>
        <van-cell title="接诊人数">
          <template #value>
            <span class="highlight">{{ doctorInfo.consultationCount || 0 }}</span>人
          </template>
        </van-cell>
        <van-cell title="性别">
          <template #value>
            <span class="highlight">{{ doctorInfo.gender === '1' ? '男' : '女' ||  '未知' }}</span>年
          </template>
        </van-cell>
      </van-cell-group>
    </div>

    <!-- 医生介绍 -->
    <div class="intro-card">
      <van-cell-group inset>
        <van-cell title="医生专长">
          <template #label>
            <div class="intro-text">
              <span>{{ doctorInfo.specialty || '暂无信息' }}</span>
            </div>
          </template>
        </van-cell>
      </van-cell-group>
    </div>

    <!-- 底部操作栏 -->
    <div class="action-bar">
      <van-button
        type="primary"
        block
        :disabled="!doctorInfo.online"
        @click="handleConsult"
      >
        {{ doctorInfo.online ? '立即问诊' : '医生不在线' }}
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast, showConfirmDialog } from 'vant'
import { getDoctorDetail, createConsultation } from '@/api/doctor'
import defaultAvatar from '@/assets/default-avatar.png'

const route = useRoute()
const router = useRouter()
const doctorId = route.params.id
const doctorInfo = ref({})

// 加载医生详情
const loadDoctorDetail = async () => {
  try {
    const res = await getDoctorDetail(doctorId)
    if (res.code === 200) {
      doctorInfo.value = res.data
    } else {
      showToast(res.message || '获取医生信息失败')
    }
  } catch (error) {
    console.error('获取医生信息失败:', error)
    showToast('获取医生信息失败')
  }
}

// 发起问诊
const handleConsult = async () => {
  try {
    const res = await showConfirmDialog({
      title: '提示',
      message: '是否确认向该医生发起问诊？',
    })

    if (res === 'confirm') {
      const result = await createConsultation(doctorId)
      if (result.code === 200) {
        router.push(`/consultation/chat/${result.data}`)
      } else {
        showToast(result.message || '发起问诊失败')
      }
    }
  } catch (error) {
    console.error('发起问诊失败:', error)
    showToast('发起问诊失败')
  }
}

onMounted(() => {
  loadDoctorDetail()
})
</script>

<style lang="scss" scoped>
.doctor-detail {
  min-height: 100vh;
  background-color: #f7f8fa;
  padding: 46px 0 80px;

  .doctor-card {
    margin: 12px 0;

    .doctor-info {
      display: flex;
      align-items: center;
      gap: 8px;

      .name {
        font-size: 16px;
        font-weight: 500;
      }
    }

    .subtitle {
      font-size: 14px;
      color: #666;
      margin-top: 4px;
    }

    .status {
      margin-top: 8px;
    }
  }

  .stats-card {
    margin: 12px 0;

    .highlight {
      color: #ff6b6b;
      font-weight: 500;
      margin-right: 4px;
    }
  }

  .intro-card {
    margin: 12px 0;

    .intro-text {
      font-size: 14px;
      color: #666;
      line-height: 1.6;
      padding: 8px 0;
    }
  }

  .action-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 12px 16px;
    background: #fff;
    box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.05);

    .van-button {
      border-radius: 4px;
    }
  }
}
</style>
